package com.gxa.utils;

public class ResultUtils {


    /**
     * 构造失败消息
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result buildFail(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 构建失败信息
     *
     * @param messageCode
     * @return
     */
    public static Result buildFail(MessageCode messageCode) {
        return buildFail(messageCode.getCode(), messageCode.getMsg());
    }


    /**
     * 构建列表的请求成功
     *
     * @param data
     * @param count
     * @return
     */
    public static Result buildSuccess(Object data, Long count) {
        Result result = new Result();
        result.setCode(MessageCode.REQUEST_SUCCESS.getCode());
        result.setMsg(MessageCode.REQUEST_SUCCESS.getMsg());
        result.setData(data);
        result.setCount(count);
        return result;
    }

    /**
     * 避免warning,在Result类泛型中指定返回类型,直接返回build(obj.method())
     * @author IKE <hp1041735092@outlook.jp>
     * @date 23/2/2024
     */
    public static <T> CustomResult<T> buildSuccessResponse(T data) {
        CustomResult<T> result = new CustomResult<>();
        result.setCode(MessageCode.REQUEST_SUCCESS.getCode());
        result.setMsg(MessageCode.REQUEST_SUCCESS.getMsg());
        result.setData(data);

        return result;
    }

    /**
     * 构建请求成功单个数据
     *
     * @param data
     * @return
     */
    public static Result buildSuccess(Object data) {
        return buildSuccess(data, null);
    }

    /**
     * 构建没有携带数据的请求成功
     *
     * @return
     */
    public static Result buildSuccess() {
        return buildSuccess(null, null);
    }

}